#!/bin/bash
device=$1
item=$2

if [ "${device}" = "whole" ];
then
    case $item in
	unreach)
        targets=(`cat /usr/local/zabbix/hostip.dat |wc -l`)
	alive=$(fping -s -a  -c 3 -t 2500 -f /usr/local/zabbix/hostip.dat 2>&1 |grep "alive" |awk '{print$1}')
	unreachable=$((${targets}-${alive}))
	echo ${unreachable}
	;;

	per)
	targets=$(cat /usr/local/zabbix/hostip.dat |wc -l)
        alive=$(fping -s -a  -c 3 -t 2500 -f /usr/local/zabbix/hostip.dat 2>&1 |grep "alive" |awk '{print$1}') 
        unreachable=$((${targets}-${alive}))
	awk 'BEGIN{printf "%.2f\n",('$unreachable'/'$targets')*100}'
	;;

	time)
	time_avg=$(fping -s -a -c 3 -t 2500 -f /usr/local/zabbix/hostip.dat 2>&1 |grep "(avg round trip time)"|awk '{print $1}')
	if [[ "$time_avg" == "0.00" ]]
	then
  	    time_avg=2500
	fi
	echo $time_avg
	time_avg=$(fping -s -a -c 3 -t 2500 $device 2>&1 |grep "(avg round trip time)"|awk '{print $1}')
	;;

    esac
else
	case $item in
        time)
	time_avg=$(fping -s -a -c 3 -t 2500 $device 2>&1 |grep "(avg round trip time)"|awk '{print $1}')
        if [[ "$time_avg" == "0.00" ]]
        then
            time_avg=2500
        fi
        echo $time_avg
        ;;

    esac
fi
